<script setup lang="ts">
import { deleteRoleDataAPI, getAllRoleDataAPI, getContactUserDataAPI, getUserPermissionDataAPI, userDataAPI } from '@/apis/system';
import type { AllRoleData, Row, UserData } from '@/types/system';
import { onMounted, ref } from 'vue';
import userIcon from '@/assets/user.svg'
import userIconActive from '@/assets/user-active.svg'
import { ElMessage, ElMessageBox, type TreeInstance } from 'element-plus';

//用户列表
const userData = ref<UserData[]>([])
//获取用户列表
const userDataGet = async () => {
  const res = await userDataAPI()
  userData.value = res.data
  const id = userData.value[0].roleId
  getUserRermissionData(id as number)
  getContactUserData(id as number)
}
//角色所有用户列表
const contactUserList = ref<Row[]>([])
//获取当前角色下关联的用户列表
const getContactUserData = async (id: number) => {
  const res = await getContactUserDataAPI(id)
  contactUserList.value = res.data.rows
}
//删除角色
const delRole=async(id:number)=>{
  ElMessageBox.confirm('此操作将永久删除该角色, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
  .then(async() => {
    await deleteRoleDataAPI(id)
    userDataGet()
    ElMessage.success('删除成功')
  })
  .catch(() => {})
}
onMounted(() => {
  userDataGet()
  getAllRoleData()
})
//选择角色数据数组下标
const activeIndex = ref(0)
//默认选中的权限
const defaultCheckedKeys = ref<number[][]>([])
//切换角色时进行的操作
const changeRole = async (index: number, id: number) => {
  activeIndex.value = index
  getUserRermissionData(id)
  getContactUserData(id)
}
//获取每个用户权限
const getUserRermissionData = async (id: number) => {
  const res = await getUserPermissionDataAPI(id);
  defaultCheckedKeys.value = res.data.perms
  treeRefs.value?.forEach((tree, index) => {
    tree.setCheckedKeys(defaultCheckedKeys.value[index])
  })
}
// 所有权限
const allRoleData = ref<AllRoleData[]>([])
//获取所有权限
const getAllRoleData = async () => {
  const res = await getAllRoleDataAPI()
  allRoleData.value = res.data
}
//树形权限
const treeRefs = ref<TreeInstance[]>()
</script>
<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div class="role-item" v-for="(item, index) in userData" :key="item.roleId"
        @click="changeRole(index, item.roleId as number)" :class="{ active: activeIndex === index }">
        <div class="role-info">
          <img :src="index === activeIndex ? userIconActive : userIcon" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <img src="@/assets/more.svg" class="icon" />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="$router.push(`/addrole/${item.roleId}`)">编辑角色</el-dropdown-item>
                <el-dropdown-item @click="delRole(item.roleId as number)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="small" @click="$router.push('/addrole')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs>
        <el-tab-pane label="权限列表">
          <div class="tree-wrapper">
            <!-- 角色权限列表 -->
            <div v-for="item in allRoleData" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <!-- 树形组件 -->
              <el-tree :data="item.children" :props="{ label: 'title', disabled: () => true }" default-expand-all
                show-checkbox node-key="id" ref="treeRefs" />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="用户列表">
          <div class="user-wrapper">
            <el-table :data="contactUserList">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
        width: 20px;
      }
    }

    .more {
      display: flex;
      align-items: center;

      .icon {
        width: 15px;
      }
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>